package com.liuweiheng.cloudproviderpayment8002.common.service.impl;

import com.liuweiheng.base.entity.R;
import com.liuweiheng.cloudproviderpayment8002.common.service.BaseService;
import com.liuweiheng.cloudproviderpayment8002.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service()
public class BaseServiceImpl implements BaseService {


    /**
     * 日志对象
     */
    private Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);


    @Override
    public R login(User user) {

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            logger.info("用户认证失败");
            return R.error();
        }

        if (subject.isAuthenticated()) {
            logger.info("用户认证成功");
            // 记录缓存数据
            return R.ok();
        }

        return R.error();
    }
}
